package cn.sciento.farm.devicemanager.gateway.uplink.service.base;


import cn.sciento.farm.devicemanager.common.util.SpringContextHolder;
import cn.sciento.farm.devicemanager.dao.entity.*;
import cn.sciento.farm.devicemanager.dao.mapper.ControllerLogMapper;
import cn.sciento.farm.devicemanager.dao.mapper.TimerLogMapper;
import cn.sciento.farm.devicemanager.sensor.Controller;
import cn.sciento.farm.devicemanager.service.ControllerLogService;
import cn.sciento.farm.devicemanager.service.DeviceStatusService;
import cn.sciento.farm.devicemanager.service.StrategyLogService;
import cn.sciento.farm.devicemanager.service.TimerLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;

/**
 * User: wumu
 * Date: 11/27/17
 * Time: 11:49 AM
 */
public class LogTaskFactory {
    private static Logger logger = LoggerFactory.getLogger(LogTaskFactory.class);
    private static DeviceStatusService deviceStatusLogService = SpringContextHolder.getBean(DeviceStatusService.class);
    private static ControllerLogService controllerLogService = SpringContextHolder.getBean(ControllerLogService.class);
    private static StrategyLogService strategyLogService = SpringContextHolder.getBean(StrategyLogService.class);
    private static TimerLogService timerLogService = SpringContextHolder.getBean(TimerLogService.class);

    public static TimerTask logOnline(DevDevice device, String node, long time, int status) {
        return new TimerTask() {
            @Override
            public void run() {
                String conNumber = device.getId();
                DevDeviceStatus devDeviceStatusLog = new DevDeviceStatus();
                devDeviceStatusLog.setEui(conNumber);
                devDeviceStatusLog.setNode(node);
                devDeviceStatusLog.setName(device.getName());
                devDeviceStatusLog.setStartTime(time);
                devDeviceStatusLog.setStatus(status);
                deviceStatusLogService.insert(devDeviceStatusLog);
            }
        };
    }

    public static TimerTask saveControllerLog(DevControllerLog controllerLog) {
        return new TimerTask() {
            @Override
            public void run() {
               controllerLogService.insert(controllerLog);
            }
        };
    }


    public static TimerTask saveStrategyLog(DevStrategyLog strategyLog) {
        return new TimerTask() {
            @Override
            public void run() {
                strategyLogService.insert(strategyLog);
            }
        };
    }

    public static TimerTask saveTimerLog(DevTimerLog switchTimerLog) {
        return new TimerTask() {
            @Override
            public void run() {
                timerLogService.insert(switchTimerLog);
            }
        };
    }
}
